package com.jic.product.controller.store;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.model.entity.MStore;
import com.jic.product.model.vo.request.MStoreRequest;
import com.jic.product.model.vo.response.MStoreResponse;
import com.jic.product.service.MStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-04-24 16:04:24
 */
@RestController
@Slf4j
@RequestMapping("/mStore")
@Api(tags = " [实体仓库表]控制类-PC")
public class MStoreController {
    @Autowired
    private MStoreService service;

    /**
     * 新增记录
     *
     * @param request 新增[实体仓库表]请求参数
     * @return MStoreResponse 新增[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[实体仓库表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.save(mStore);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[实体仓库表]请求参数
     * @return MStoreResponse 新增或修改[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[实体仓库表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.saveOrUpdate(mStore);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[实体仓库表]请求参数
     * @return MStoreResponse 删除[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[实体仓库表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.deleteByPrimaryKeyLogically(mStore);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[实体仓库表]请求参数
     * @return MStoreResponse 修改[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[实体仓库表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.updateByPrimaryKeySelective(mStore);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     *
     * @return MStoreResponse 修改[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[实体仓库表]记录状态")
    @PostMapping("/updateStatus")
    @ResponseBody
    public RestResult<Long> updateStatus(@RequestBody MStoreRequest request) throws Throwable {
        long flag = 0;
        Long[] ids = request.getIds();
        for (Long id : ids) {
            MStore mStore = new MStore();
            mStore.setId(id);
            mStore.setStatus(request.getStatus());
            flag = service.updateByPrimaryKeySelective(mStore);
        }
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[实体仓库表]详情请求参数
     * @return MStoreResponse 查询[实体仓库表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[实体仓库表]详情")
    @ResponseBody
    public RestResult<MStoreResponse> getByPrimaryKey(@Validated @RequestBody MStoreRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            MStore mStore = new MStore();
            BeanUtils.copyProperties(request, mStore);
            MStore result = service.getByPrimaryKey(mStore);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            MStoreResponse bo = new MStoreResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(null);
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<MStoreResponse>> listPage(@RequestBody MStoreRequest request) throws Throwable {
        MStore param = new MStore();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listMStoreStock")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<MStoreResponse>> listMStoreStock(@RequestBody MStoreRequest request) throws Throwable {
        return RestResult.success(service.listMStoreStock(request, request.getPageNum(), request.getPageSize()));
    }

    /**
     * 不分页查询--实体库存查询导出
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listMStoreStockExcel")
    @ApiOperation(value = " 不分页查询--实体库存查询导出 ")
    @ResponseBody
    public RestResult<List<MStoreResponse>> listMStoreStockExcel(@RequestBody MStoreRequest request) throws Throwable {
        return RestResult.success(service.listMStoreStockExcel(request));
    }

    /**
     * 库存预警值分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listMStoreWarning")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<MStore>> listMStoreWarning (@RequestBody MStoreRequest request) throws Throwable {
        MStore param = new MStore();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);return RestResult.success(service.listMStoreWarning(param, request.getPageNum(),request.getPageSize()));
    }

    /**
     * M_STORE 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [实体仓库表] ")
    @ResponseBody
    public RestResult<List<MStoreResponse>> listMStoreAllRecord(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        List<MStore> list = service.listAllRecord(mStore);
        List<MStoreResponse> responseList = list.stream().map(temp -> {
            MStoreResponse bo = new MStoreResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * M_STORE 库存预警查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAllWarning")
    @ApiOperation(value = "不分页查询 [实体仓库表] ")
    @ResponseBody
    public RestResult<List<MStoreResponse>> listAllWarning(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        List<MStore> list = service.listAllWarning(mStore);
        List<MStoreResponse> responseList = list.stream().map(temp -> {
            MStoreResponse bo = new MStoreResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 通过商家/平台/店铺id/仓库名称/仓库编码查询
     *
     * @param request 查询[实体仓库表]详情请求参数
     * @return MStoreResponse 查询[实体仓库表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByMerchantId")
    @ApiOperation(value = "查询[实体仓库表]详情")
    @ResponseBody
    public RestResult<List<MStoreResponse>> getByMerchantId(@Validated @RequestBody MStoreRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        List<MStore> result = service.getByMerchantId(mStore);
        List<MStoreResponse> responseList = result.stream().map(temp -> {
            MStoreResponse bo = new MStoreResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 新增记录
     *
     * @param request 新增[实体仓库表]请求参数
     * @return MStoreResponse 新增[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[实体仓库表]记录")
    @PostMapping("/saveMStore")
    @ResponseBody
    public RestResult<Long> saveMStore(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.saveMStore(mStore);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[实体仓库表]请求参数
     * @return MStoreResponse 修改[实体仓库表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[实体仓库表]记录")
    @PostMapping("/updateMStore")
    @ResponseBody
    public RestResult<Long> updateMStore(@RequestBody MStoreRequest request) throws Throwable {
        MStore mStore = new MStore();
        BeanUtils.copyProperties(request, mStore);
        long flag = service.updateByPrimaryKeySelective(mStore);
        return RestResult.success(flag);
    }
}

